import 'package:flutter/material.dart';
import 'package:fly/app/modules/home/views/dot_render_controller.dart';
import 'package:get/get.dart';

class DotScreenView extends StatefulWidget {
  const DotScreenView({this.controller, Key? key}) : super(key: key);
  final DotRenderController? controller;

  @override
  State<DotScreenView> createState() => _DotScreenViewState();
}

class _DotScreenViewState extends State<DotScreenView> {
  List<Widget> dots = [];

  @override
  initState() {
    super.initState();
    refreshDots();
  }

  refreshDots() {
    if (widget.controller != null) {
      dots.clear();
      for (int i = 0; i < DotRenderController.row; i++) {
        for (int j = 0; j < DotRenderController.col; j++) {
          dots.add(Obx(() => Container(
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(50),
                  color: widget.controller!.dotRenderColors[j][i] == 1
                      ? Colors.brown
                      : Colors.grey.withOpacity(.2),
                ),
              )));
        }
      }

      widget.controller!.initDotScreenData();
      if (widget.controller!.autoScroll == true) {
        widget.controller!.scrollShowTxt();
      }
    }
  }

  @override
  void dispose() {
    widget.controller?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        widget.controller?.press();
      },
      child: SizedBox(
        width: 280,
        child: GridView.count(
          crossAxisCount: 26,
          childAspectRatio: 1,
          mainAxisSpacing: 1,
          crossAxisSpacing: 1,
          shrinkWrap: true,
          children: [...dots],
        ),
      ),
    );
  }
}
